'use strict';

angular.module('CBO').controller('DepartmentController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', '$location', 'Authentication',
    function($scope, $stateParams, BaseModule, BaseFunction, $location, Authentication) {
        //$scope.authentication = Authentication;
        
        $scope.btnSave = function(){
            if($scope.Module.Department.FocusedRecord.Code == undefined || 
                $scope.Module.Department.FocusedRecord.Code.trim() == ""){
                BaseFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.Department.FocusedRecord.Name == undefined || 
                $scope.Module.Department.FocusedRecord.Name.trim() == ""){
                BaseFunction.alert("名称不能为空！", "保存错误");
                return;
            }

            $scope.Module.Department.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.Department.cancel();            
        };
        $scope.btnAdd = function(){
            $scope.Module.Department.add();
            $scope.Module.Department.FocusedRecord.IsEffective = true;
        };
        $scope.btnDelete = function(){
            $scope.Module.Department.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/Department/List');
        };

        //初始化
        BaseModule.initModule($scope, 'Department');
        $scope.Module.Department.Populate = [{path: 'UpDepartment', select: 'Code Name'}, {path: 'ParentDepartment', select: 'Code Name'}];

        if($stateParams.id) {
            $scope.Module.Department.findById($stateParams.id, function(record){
                $scope.Module.Department.FocusedRecord = record;
            });
        }
        else{
            $scope.btnAdd();
        };
    }
]);
